import {
  View,
  Modal,
  StyleSheet,
  Animated,
  TouchableOpacity,
} from 'react-native';
import React, {useEffect, useMemo, useRef, useState} from 'react';
import BottomModalCell from './BottomModalCell';
const menuList = [
  {title: 'Save To Later', icon: 'share'},
  {title: 'Save To Playlist', icon: 'share'},
  {title: 'Share', icon: 'share'},
  {title: 'Not interested', icon: 'share'},
  {title: 'Report', icon: 'share'},
];
interface BottomMenuProps {
  modalVisible: boolean;
  onClickClose: () => void;
}
const modalHeight = 300;
const BottomMenu = (props: BottomMenuProps) => {
  const {modalVisible} = props;
  const translateY = useRef(
    new Animated.Value(modalVisible ? modalHeight : 0),
  ).current;
  const fadeAnim = useRef(new Animated.Value(modalVisible ? 0 : 0.5)).current;

  useEffect(() => {
    Animated.sequence([
      Animated.timing(translateY, {
        toValue: modalVisible ? 0 : modalHeight,
        duration: 300,
        delay: modalVisible ? 0 : 300,
        useNativeDriver: false,
      }),
      Animated.timing(fadeAnim, {
        toValue: modalVisible ? 0.5 : 0,
        delay: modalVisible ? 100 : 0,
        duration: 100,
        useNativeDriver: false,
      }),
    ]).start();
  }, [modalVisible]);
  return (
    <Modal
      transparent={true}
      visible={modalVisible}
      onRequestClose={props.onClickClose}>
      <TouchableOpacity
        style={{flex: 1, backgroundColor: 'transparent'}}
        activeOpacity={1}
        onPress={props.onClickClose}>
        <Animated.View style={[styles.overlay, {opacity: fadeAnim}]} />
      </TouchableOpacity>
      <Animated.View style={[styles.container, {transform: [{translateY}]}]}>
        <View style={styles.content}>
          {menuList.map((item, index) => {
            return (
              <BottomModalCell item={item.title} icon={item.icon} key={index} />
            );
          })}
        </View>
      </Animated.View>
    </Modal>
  );
};

const styles = StyleSheet.create({
  overlay: {
    flex: 1,
    backgroundColor: 'rgb(0, 0, 0)',
  },
  container: {
    position: 'absolute',
    bottom: 0,
    width: '100%',
    height: '40%',
    backgroundColor: '#212121',
  },
  content: {
    flex: 1,
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
  },
  subtitle: {
    fontSize: 16,
    color: 'gray',
  },
});
export default BottomMenu;
